<template>
  <div class="admin-container">
    <!-- 侧边栏导航 -->
    <aside class="sidebar">
      <div class="logo">Admin Panel</div>
      <nav>
        <router-link 
          v-for="item in menuItems" 
          :key="item.path" 
          :to="item.path" 
          class="nav-item"
          :class="{ active: isActive(item.path) }"
        >
          {{ item.title }}
        </router-link>
      </nav>
    </aside>

    <!-- 主内容区 -->
    <main class="content">
      <!-- 顶部导航 -->
      <header class="header">
        <div class="header-left">
          <h2>{{ currentPageTitle }}</h2>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="30" style="margin-right: 10px">
                {{ auth.currentUser?.name?.charAt(0) }}
              </el-avatar>
              {{ auth.currentUser?.name }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="router.push('/admin/settings')">
                  个人设置
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </header>

      <!-- 内容区域 -->
      <div class="main-content">
        <router-view />
      </div>
    </main>
  </div>
  <div v-if="$route.path === '/admin/audit'" class="audit-section">
    <h3>待审内容</h3>
    <div v-for="item in pendingItems" :key="item.id" class="audit-item">
      <p>{{ item.content }}</p>
      <div class="audit-actions">
        <el-button type="success" @click="approve(item.id)">通过</el-button>
        <el-button type="danger" @click="reject(item.id)">拒绝</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const auth = useAuthStore()

const menuItems = [
  { path: '/admin/dashboard', title: '数据看板' },
  { path: '/admin/users', title: '用户管理' },
  { path: '/admin/audit', title: '内容审核' },
  { path: '/admin/settings', title: '系统设置' }
]

const currentPageTitle = computed(() => {
  return menuItems.find(item => item.path === route.path)?.title || '管理后台'
})

const isActive = (path) => {
  return route.path === path
}

const handleLogout = () => {
  auth.logout()
  router.push('/login')
}

const pendingItems = ref([
  { id: 1, content: '课程《人工智能基础》申请上线' },
  { id: 2, content: '用户「李老师」权限升级申请' }
])

const approve = (id) => {
  pendingItems.value = pendingItems.value.filter(item => item.id !== id)
}

const reject = (id) => {
  pendingItems.value = pendingItems.value.filter(item => item.id !== id)
}
</script>

<style scoped>
.admin-container {
  display: flex;
  height: 100vh;
  background: #f0f2f5;
}

.sidebar {
  width: 240px;
  background: #fff;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.1);
  transition: width 0.3s;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
  border-bottom: 1px solid #e8e8e8;
}

.nav-item {
  display: block;
  padding: 14px 24px;
  color: #333;
  text-decoration: none;
  transition: all 0.3s;
}

.nav-item:hover {
  background: #f5f7fa;
  color: #409eff;
}

.nav-item.active {
  background: #ecf5ff;
  color: #409eff;
  border-right: 3px solid #409eff;
}

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  background: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 12px;
}

.main-content {
  flex: 1;
  padding: 24px;
  background: #fff;
  margin: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.audit-section {
  padding: 24px;
  background: #fff;
  margin: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.audit-item {
  padding: 16px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  margin-bottom: 16px;
}

.audit-actions {
  margin-top: 16px;
}
</style>